using UnityEngine;

public class NPCController : MonoBehaviour
{
    FoodObj m_FoodTarget;
    QuiQuiObj m_QuiQuiObj;
    // Start is called before the first frame update
    void Start()
    {
        m_QuiQuiObj = GetComponent<QuiQuiObj>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void FixedUpdate()
    {
        if (!m_FoodTarget || !m_FoodTarget.gameObject.activeSelf)
        {
            CheckFood();
        }
        if (m_FoodTarget)
        {
            var dir = (m_FoodTarget.transform.position - transform.position).normalized;
            m_QuiQuiObj.Move(dir);
        }
    }

    void CheckFood()
    {
        FoodObj closeFood = null;
        float minDistanceSqr = Mathf.Pow(1000, 2f);
        var smallFood = IntersectionObjManager.Instance.GetIntersectionObjects(IntersectionObjManager.IntersectionObjType.SmallFood);
        foreach (var i in smallFood)
        {
            var difference = i.transform.position - transform.position;
            var distanceSqr = difference.sqrMagnitude;
            if (minDistanceSqr > distanceSqr)
            {
                closeFood = i as FoodObj;
                minDistanceSqr = distanceSqr;
            }
        }
        if (closeFood)
            m_FoodTarget = closeFood;
    }
}
